home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / pending.js < prev    next >
Text File  |  2007-11-11  |  4KB  |  192 lines

  1. /*
  2.     pending.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_pending =
  8. {
  9.     init: function()
  10.     {
  11.     },
  12.  
  13.     store: function(name) /* Hostname */
  14.     {
  15.         try {
  16.             if (!wot_cache.iscached(name) ||
  17.                     !wot_cache.get(name, "pending")) {
  18.                 return false;
  19.             }
  20.  
  21.             var testimony_url = wot_cache.get(name, "testimony_url");
  22.  
  23.             if (!testimony_url) {
  24.                 return false;
  25.             }
  26.  
  27.             var str = encodeURIComponent(
  28.                         wot_url.replacehostname(
  29.                             testimony_url,
  30.                             wot_idn.utftoidn(name)))
  31.  
  32.             dump("wot_pending.store: " + str + "\n");
  33.  
  34.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  35.                 str += " " + wot_cache.get(name, "testimony_" + i);
  36.             }
  37.  
  38.             var pending = "." + Date.now().toString();
  39.  
  40.             return (wot_prefs.setChar("pending", wot_prefs.pending +
  41.                         pending + " ") &&
  42.                     wot_prefs.setChar("pending" + pending, str));
  43.         } catch (e) {
  44.             dump("wot_pending.store: failed with " + e + "\n");
  45.         }
  46.  
  47.         return false;
  48.     },
  49.  
  50.     clear: function(name) /* Preferences entry */
  51.     {
  52.         try {
  53.             if (!name || name.charAt(0) != '.') {
  54.                 return;
  55.             }
  56.  
  57.             dump("wot_pending.clear: clearing " + name + "\n");
  58.  
  59.             /* Remove entry from preferences */
  60.             var pending = wot_prefs.pending.replace(RegExp(name), "");
  61.             pending = pending.replace(/^\s*/, ""); /* Leading whitespace */
  62.  
  63.             var entry = "pending" + name;
  64.  
  65.             if (wot_prefs.getChar(entry, null)) {
  66.                 wot_prefs.clear(entry);
  67.                 wot_prefs.clear(entry + ".submit");
  68.                 wot_prefs.clear(entry + ".tries");
  69.                 wot_prefs.setChar("pending", pending);
  70.             }
  71.         } catch (e) {
  72.             dump("wot_pending.clear: failed with " + e + "\n");
  73.         }
  74.     },
  75.  
  76.     submit: function()
  77.     {
  78.         try {
  79.             var processing = wot_prefs.pending;
  80.  
  81.             while (processing != "") {
  82.                 /* Find an entry containing pending testimonies */
  83.                 var result = /^\s*(\.\d+)\s*(.*)/.exec(processing);
  84.  
  85.                 if (!result || !result[1]) {
  86.                     dump("wot_pending.submit: invalid format for: " +
  87.                         processing + "\n");
  88.                     break;
  89.                 } else if (result[2]) {
  90.                     processing = result[2];
  91.                 } else {
  92.                     processing = "";
  93.                 }
  94.  
  95.                 var cn = result[1];
  96.  
  97.                 /* See if such entry exists and get its contents */
  98.                 var content = wot_prefs.getChar("pending" +    cn, null);
  99.  
  100.                 if (!content) {
  101.                     continue;
  102.                 }
  103.  
  104.                 /* Check status */
  105.                 var submit = wot_prefs.getChar("pending" + cn + ".submit",
  106.                                     null);
  107.  
  108.                 if (submit && (Date.now() - Number(submit)) <
  109.                                 WOT_INTERVAL_SUBMIT_ERROR) {
  110.                     /* Don't resubmit yet */
  111.                     continue;
  112.                 }
  113.  
  114.                 var tries = wot_prefs.getChar("pending" + cn + ".tries", null);
  115.  
  116.                 if (tries) {
  117.                     tries = Number(tries);
  118.                     if (tries >= WOT_MAX_TRIES_SUBMIT) {
  119.                         this.clear(cn); /* Didn't work out... */
  120.                         continue;
  121.                     }
  122.                 } else {
  123.                     tries = 0;
  124.                 }
  125.  
  126.                 /* Parse target URL */
  127.                 var m = /^([^\s]+)(.*)/.exec(content);
  128.  
  129.                 if (!m || !m[1] || !m[2]) {
  130.                     dump("wot_pending.submit: invalid entry: " + cn + "\n");
  131.                     this.clear(cn);
  132.                     continue;
  133.                 }
  134.  
  135.                 var target = decodeURIComponent(m[1]);
  136.  
  137.                 if (!wot_url.ishostname(
  138.                         wot_url.gethostname(target))) {
  139.                     dump("wot_pending.submit: invalid entry: " + cn + "\n");
  140.                     this.clear(cn);
  141.                     continue;
  142.                 }
  143.  
  144.                 /* Parse testimonies */
  145.                 var testimonies = new Array();
  146.                 content = m[2];
  147.  
  148.                 for (var j = 0; j < WOT_APPLICATIONS; ++j) {
  149.                     m = /^\s*(-?\d+)(.*)/.exec(content);
  150.  
  151.                     if (!m || m[1] == null || Number(m[1]) < 0) {
  152.                         testimonies[j] = -1;
  153.                     } else if (Number(m[1]) > WOT_MAX_REPUTATION) {
  154.                         testimonies[j] = WOT_MAX_REPUTATION;
  155.                     } else {
  156.                         testimonies[j] = Number(m[1]);
  157.                     }
  158.                     content = m[2];
  159.                 }
  160.  
  161.                 dump("wot_pending.submit: found a pending testimony for " +
  162.                     target + "\n");
  163.  
  164.                 /* Update status, submit testimonies */
  165.                 wot_prefs.setChar("pending" + cn + ".submit",
  166.                     Date.now().toString());
  167.                 wot_prefs.setChar("pending" + cn + ".tries",
  168.                     (tries + 1).toString());
  169.                 wot_api_submit.send(cn, target, testimonies);
  170.  
  171.                 /* If there is cached data for this item, update cached
  172.                    testimonies unless they have been already changed again */
  173.                 var hostname = wot_url.gethostname(target);
  174.  
  175.                 if (wot_cache.iscached(hostname) &&
  176.                         !wot_cache.get(hostname, "pending")) {
  177.                     for (var k = 0; k < WOT_APPLICATIONS; ++k) {
  178.                         if (testimonies[k] >= 0) {
  179.                             wot_cache.set(hostname, "testimony_" + k,
  180.                                 testimonies[k]);
  181.                         }
  182.                     }
  183.                 }
  184.             }
  185.         } catch (e) {
  186.             dump("wot_pending.submit: failed with " + e + "\n");
  187.         }
  188.     }
  189. };
  190.  
  191. wot_pending.init();
  192.